<?php

namespace backend\models;

use Yii;
use yii\base\Model;
use yii\data\ActiveDataProvider;
use backend\models\EduFeedback;

/**
 * EduFeedbackSearch represents the model behind the search form of `backend\models\EduFeedback`.
 */
class EduFeedbackSearch extends EduFeedback
{

    public $create_time_start;

    public $create_time_end;
    /**
     * {@inheritdoc}
     */
    public function rules()
    {
        return [
            [['id', 'user_id', 'create_time', 'status'], 'integer'],
            [['desc', 'contact'], 'safe'],
            [['create_time_start','create_time_end'], 'string']
        ];
    }

    /**
     * {@inheritdoc}
     */
    public function scenarios()
    {
        // bypass scenarios() implementation in the parent class
        return Model::scenarios();
    }

    /**
     * Creates data provider instance with search query applied
     *
     * @param array $params
     *
     * @return ActiveDataProvider
     */
    public function search($params)
    {
        $query = EduFeedback::find();

        // add conditions that should always apply here

        $dataProvider = new ActiveDataProvider([
            'query' => $query,
        ]);

        $this->load($params);

        if (!$this->validate()) {
            // uncomment the following line if you do not want to return any records when validation fails
            // $query->where('0=1');
            return $dataProvider;
        }

        // grid filtering conditions
        $query->andFilterWhere([
            'id' => $this->id,
            'user_id' => $this->user_id,
            'status' => $this->status,
//            'create_time' => $this->create_time,
        ]);

        if ($this->create_time_start != '' && $this->create_time_end == '') {
            $query->andFilterWhere(['>=', 'create_time', strtotime($this->create_time_start)]);
        }

        if ($this->create_time_start == '' && $this->create_time_end != '') {
            $query->andFilterWhere(['<=', 'create_time', strtotime($this->create_time_end)]);
        }


        if ($this->create_time_start != '' && $this->create_time_end != '') {
            $query->andFilterWhere(['between', 'create_time', strtotime($this->create_time_start), strtotime($this->create_time_end) + 24 * 60 * 60]);
        }

        $query->andFilterWhere(['like', 'desc', $this->desc])
            ->andFilterWhere(['like', 'contact', $this->contact]);

        return $dataProvider;
    }
}
